<!-- subpkg_archive/list/index.vue -->
<script setup>
  import { ref } from 'vue'
  import { patientListApi, removePatientApi, patientDetailApi } from '@/services/patient.js'
  import { onShow } from '@dcloudio/uni-app'
  const swipeOptions = ref([
	  {
		text: '删除',
		style: {
			backgroundColor: '#dd524d'
		}
	  }
  ])
  
  const ptList = ref([])
  async function getPatientList () {
	  try {
	  	const { code, data } = await patientListApi()
	  	if (code !== 10000) return uni.utils.toast('获取家庭档案信息失败')
	  	ptList.value = data
	  } catch (err) {
	  	console.log(err, 'err');
	  	//TODO handle the exception
	  }
  }
  
  async function clickSwipe (id, index) {
	try {
		const { code, message } = await removePatientApi(id)
		if (code !== 10000) return uni.utils.toast(message)
		ptList.value.splice(index, 1)
	} catch (error) {
		console.log(error, 'error');
	}
  }
  
  onShow(() => {
	getPatientList()
  })
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action v-if="ptList.length >= 1">
        <uni-swipe-action-item 
			v-for="(pt, index) in ptList" 
			:key="pt.id" 
			:right-options="swipeOptions"
			@click="clickSwipe(pt.id, index)"
		>
          <view class="archive-card active">
            <view class="archive-info">
              <text class="name">{{ pt.name }}</text>

              <text class="id-card">{{ pt.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2') }}</text>

              <text class="default" v-if="pt.defaultFlag === 1">默认</text>

            </view>

            <view class="archive-info">
              <text class="gender">{{ pt.genderValue }}</text>

              <text class="age">{{ pt.age }}岁</text>

            </view>

            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/index?id=${pt.id}`"
            >
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
              />
            </navigator>

          </view>

        </uni-swipe-action-item>

      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="ptList && ptList.length < 6"  class="archive-card">
        <navigator
          class="add-link"
          hover-class="none"
          url="/subpkg_archive/form/index"
        >
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>

        </navigator>

      </view>

    </view>

  </scroll-page>

</template>

<style lang="scss">
  @import './index.scss';
</style>
